﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace ApplicationSecurityService
{
    class ProcessManager : Singleton<ProcessManager>
    {
        public IList<ProcessInfo> checkProcesss = new List<ProcessInfo>();
        private Dictionary<String, ProcessInfo> dictProcess = new Dictionary<string, ProcessInfo>();
        public void Init()
        {

        }

        internal void SetCheckProcess(IList<ProcessInfo> list)
        {
            checkProcesss = list;
            dictProcess.Clear();
            foreach (var prcessInfo in checkProcesss)
            {
                // 测试日志,可以去除
                LogHelper.Instance.WriteLog("checkeItem:" + prcessInfo.ProcessName + " md5:" + prcessInfo.FileMd5Code);

                if (!dictProcess.ContainsKey(prcessInfo.ProcessName))
                {
                    dictProcess.Add(prcessInfo.ProcessName, prcessInfo);
                }
            }
        }

        // 进程扫描
        public void ProcessSecurity()
        {
            Process[] pros = Process.GetProcesses();
            foreach (var item in pros)
            {
                LogHelper.Instance.WriteLog("processName:" + item.ProcessName);
                try
                {
                    LogHelper.Instance.WriteLog("fileName: " + item.MainModule.FileName + " runTime: " + DateTime.Now.ToString("HH:mm:ss"));
                    if (dictProcess.ContainsKey(item.ProcessName))
                    {
                        String checkFileMD5 = Common.GetFileMD5(item.MainModule.FileName);

                        // 测试日志,可以去除
                        LogHelper.Instance.WriteLog("checkFileMD5: " + checkFileMD5);

                        ProcessInfo ps;
                        bool result = dictProcess.TryGetValue(item.ProcessName, out ps);
                        if(result)
                        {
                            if(ps.FileMd5Code != ""  && ps.FileMd5Code != checkFileMD5)
                            {
                                // MD5错误的处理
                                // 终止进程
                                // 上报
                                
                            }
                        }

                    }
                }
                catch { continue; }
            }
        }

        // 上报
        private void ReportProcessError(String pName, String errorMd5, String normalMd5)
        {
            try
            {
                Dictionary<String, String> dParams = new Dictionary<string, string>();
                dParams.Add("IPHost", Common.GetLocalIP());
                dParams.Add("PName", pName);
                dParams.Add("ErrMd5", errorMd5);
                dParams.Add("RightMd5", normalMd5);
                String urlParams = Common.FormatUrlParams(dParams);
                String urlRequest = "http://127.0.0.1/reportError?" + urlParams;
                Common.HttpGet(urlRequest);
            }
            catch
            {}

        }

        // 请求配置
        public String RequestConfig()
        {
            try
            {
                String urlRequest = "http://127.0.0.1/requestConfig";
                String sConfig = Common.HttpGet(urlRequest);
                return sConfig;
            }
            catch{ }
            return "";
        }
    }
}
